﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace LooseChart
{
    /// <summary>
    /// 散点图
    /// </summary>
    public class ScatterSeries : XYSeries
    {
        public ScatterSeries() : base() { }

        /// <summary>
        /// 绘制内容
        /// </summary>
        /// <param name="color">基础颜色</param>
        /// <param name="list">矢量数据流</param>
        /// <param name="dc">画布</param>
        protected override void DrawDrawingContext(Color color, List<StreamGeometry> list, DrawingContext dc)
        {
            var pa = color.A / 255d;
            var fillbrush = new SolidColorBrush(Color.FromArgb(Convert.ToByte(FillColorA * pa), color.R, color.G, color.B));
            fillbrush.Freeze();
            var strokebrush = new SolidColorBrush(Color.FromArgb(Convert.ToByte(StrokeColorA * pa), color.R, color.G, color.B));
            strokebrush.Freeze();
            Brush fill = ShowFill ? fillbrush : null;
            Pen pen = ShowStroke ? new Pen(strokebrush, StrokeThickness) { DashStyle = base.DashStyle } : null;
            foreach (var stream in list)
            {
                if (stream.IsEmpty()) continue;
                dc.DrawGeometry(fill, pen, stream);
            }
        }

        /// <summary>
        /// 获取数据源的矢量数据流
        /// </summary>
        /// <param name="chartDataSource">数据源</param>
        /// <returns></returns>
        protected override async Task UpdateStreamGeometry(CancellationToken token)
        {
            if (!this.IsLoaded || token.IsCancellationRequested || XAxis == null || YAxis == null) return;
            var points = Statistics;
            if (points == null || points.Count <= 0)
            {
                MainGeometry = null;
                return;
            }
            var pointLength = PointLength;
            try
            {
                var temp = await Task.Factory.StartNew<List<StreamGeometry>>(() =>
                {
                    StreamGeometry stream = new StreamGeometry();
                    using (StreamGeometryContext sgc = stream.Open())
                    {
                        foreach (var point in points)
                        {
                            if (token.IsCancellationRequested) return null;
                            var otherpoints = new Point[]
                            {
                                new Point(point.Point.X + pointLength, point.Point.Y),
                                new Point(point.Point.X + pointLength, point.Point.Y + pointLength),
                                new Point(point.Point.X, point.Point.Y + pointLength),
                            };
                            sgc.BeginFigure(point.Point, true, true);
                            sgc.PolyLineTo(otherpoints, true, false);
                        }
                    }
                    stream.Freeze();
                    return new List<StreamGeometry>() { stream };
                }, token);
                if (!token.IsCancellationRequested) MainGeometry = temp;
            }
            catch (TaskCanceledException e)
            {
                Debug.WriteLine(string.Concat("散点图生成矢量图线程被取消", e));
            }
        }
    }
}
